# encoding=utf-8
from flask import Flask
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from app.env import SQLALCHEMY_DATABASE_URI

# 创建数据库及连接
engine = create_engine(SQLALCHEMY_DATABASE_URI)
DBSession = sessionmaker(bind=engine)
dBSession = DBSession()

app = Flask(__name__)
from app.Controllers import TeamController, PlayerController, SeasonController
from app.Controllers.TeamController import team
from app.Controllers.PlayerController import player
from app.Controllers.SeasonController import season
from app.Controllers.HistoryController import history
from app.Vendor.Code import Code
from app.Vendor.ExceptionApi import ExceptionApi

# 注册蓝图
app.register_blueprint(team)
app.register_blueprint(player)
app.register_blueprint(season)
app.register_blueprint(history)


@app.teardown_appcontext
def shutdown_session(exception=None):
    dBSession.close()


@app.errorhandler(Exception)
def error_handler(e):
    return ExceptionApi(Code.ERROR, e)
